﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace d39
{
    public partial class GridManipulator : GridControl
    {
        public GridManipulator() { Tweaks = UrlTweaks.Default; }

        public bool IncludeHalfCells { get; set; }
        public UrlTweaks Tweaks { get; set; }

        protected override void UpdateFromGrid(bool playersOrMovesChanged, bool wallTerrainChanged)
        {
            UpdatePanel.Update();
            DataBind();
            setRowsAndCols();
        }

        private void setRowsAndCols()
        {
            var size = (IncludeHalfCells ? CurrentGrid.PixelSize / 2.0f : CurrentGrid.PixelSize) - 1;
            Cell.Attributes["style"] = "width:" + size + "px;height:" + size + "px";

            var offset = IncludeHalfCells ? CurrentGrid.PixelSize / 4.0f : 0;

            var cols = IncludeHalfCells ?
                from x in Enumerable.Range(2, CurrentGrid.Width * 2 + 1) select new HalfCoordinate(x, 0) :
                from x in Enumerable.Range(1, CurrentGrid.Width) select (HalfCoordinate)new Coordinate(x, 0);

            var rows = IncludeHalfCells ?
                from y in Enumerable.Range(2, CurrentGrid.Height * 2 + 1) select new HalfCoordinate(0, y) :
                from y in Enumerable.Range(1, CurrentGrid.Height) select (HalfCoordinate)new Coordinate(0, y);

            var script = @"
                gridManipColPos = [" + string.Join(", ", from col in cols select CurrentGrid.CoordinateToPoint(col).X - offset) + @"];
                gridManipRowPos = [" + string.Join(", ", from row in rows select CurrentGrid.CoordinateToPoint(row).Y - offset) + @"];
                gridManipColName = [" + string.Join(", ", from col in cols select "'" + col.ToHumanReadableString().TrimEnd('0') + "'") + @"];
                gridManipRowName = [" + string.Join(", ", from row in rows select "'" + row.ToHumanReadableString().Substring(1) + "'") + @"];
                distFunc = distance" + CurrentGrid.DnDVersion + @";
                distUnit = '" + (CurrentGrid.DnDVersion == DnDVersion.V35 ? "ft" : "sq") + @"';
                gridHalved = " + IncludeHalfCells.ToString().ToLower() + @";";

            ScriptManager.RegisterStartupScript(this, typeof(GridManipulator), "setRowsAndCols", script, true);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(Page, typeof(GridManipulator), "init", "initGridManipulator();", true);
        }

        protected void Command_Invoke(object sender, RepeaterCommandEventArgs e)
        {
            var coord = HalfCoordinate.ParseHumanReadable(ActiveCellHidden.Value);
            var command = GridPage.Commands.ToList()[e.Item.ItemIndex];
            command.Invoke(coord);
        }
        protected void SingleCommand_Click(object sender, EventArgs e)
        {
            var coord = HalfCoordinate.ParseHumanReadable(ActiveCellHidden.Value);
            var command = GridPage.Commands.Where(cmd => cmd.Cells.Contains(coord) || !cmd.Cells.Any()).Single();
            command.Invoke(coord);
        }

        protected string GetText(dynamic dataItem)
        {
            return dataItem.Text.Replace("{}", "<span class='active-cell'></span>");
        }
    }
}